import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    redirect: '/layout/home'
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/Register.vue')
  },
  {
    path: '/layout',
    name: 'layout',
    component: () => import('@/components/LayoutMain.vue'),
    children: [
      {
          path: 'task-management',
          name: 'task-management',
          component: () => import('@/views/task-management.vue')
      }, 
      {
          path: 'project-management/:id',
          name: 'project-management',
          component: () => import('@/views/project-management.vue')
      },
      {
          path: 'task-creat/:mode/:projectId',
          name: 'task-creat',
          props: true,
          component: () => import('@/views/task-create.vue')
      },
      {
          path: 'memeber-project/:id',
          name: 'memeber-project',
          component: () => import('@/views/MemberProjectView.vue')
      },
      {
          path: 'task-detail',
          name: 'task-detail',
          props: true,
          component: () => import('@/views/TaskDetail.vue')
      },
      {
          path: 'grading',
          name: 'grading',
          component: () => import('@/views/GradingView.vue')
      },
      {
          path: 'grade-edit',
          name: 'grade-edit',
          props: true,
          component: () => import('@/views/GradeEdit.vue')
      },
      {
          path: 'student-view',
          name: 'student-view',
          component: () => import('@/views/StudentMainView.vue')
      },
      {
          path: 'home',
          name: 'home',
          component: () => import('@/views/Home.vue')
      },
      {
          path: 'user',
          name: 'user',
          component: () => import('@/views/User.vue')
      },
      {
          path: 'notifications',
          name: 'notifications',
          component: () => import('@/views/Notifications.vue')
      },
      {
          path: 'message-detail',
          name: 'message-detail',
          props: true,
          component: () => import('@/views/MessageDetail.vue')
      },
      {
          path: 'project-selection',
          name: 'project-selection',
          component: () => import('@/views/ProjectSelection.vue')
      },
      {
          path: 'project-progress',
          name: 'project-progress',
          component: () => import('@/views/ProjectProgress.vue')
      },
      {
          path: 'topic-create',
          name: 'topic-create',
          component: () => import('@/views/TopicCreate.vue')
      },
      {
          path: 'project-view/:id',
          name: 'project-view',
          component: () => import('@/views/ProjectView.vue')
      },
      {
          path: 'showgradetask',
          name: 'showgradetask',
          component: () => import('@/views/teacher/ShowGradeTask')
      },
      {
          path: 'releaseradetask',
          name: 'releaseradetask',
          component: () => import('@/views/teacher/ReleaseGradeTask')
      },
      {
          path: 'gradeproject',
          name: 'gradeproject',
          component: () => import('@/views/student/GradeProject')
      },
      {
          path: 'showgradecase',
          name: 'showgradecase',
          component: () => import('@/views/teacher/ShowGradeCase')
      },
      {
          path: 'getgradetask',
          name: 'getgradetask',
          component: () => import('@/views/student/GetGradeTask')
      },
      {
          path: 'setgroup',
          name: 'setgroup',
          component: () => import('@/views/Grouping.vue')
      },
      {
          path: 'upgroup',
          name: 'upgroup',
          component: () => import('@/views/UpGroup.vue')
      },
      {
          path: 'groupmanage',
          name: 'groupmanage',
          component: () => import('@/views/GroupManagement.vue')
      }
    ]
  }
  // {
  //   path: '/project-management',
  //         name: 'project-management',
  //         component: () => import('@/views/project-management.vue')
  // }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

 router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next()
    // 从sessionStorage中获取保存的token值
    const tokenStr = window.localStorage.getItem('token')
    // 没有token，强制跳转到登录页
    if (!tokenStr) return next('/login')
    next()
 })

export default router
